#include "wangge.h"
#include "ui_wangge.h"
#include <QPainter>
#include <time.h>
#include <QTime>
#include <stdio.h>
#include<QPen>
#include<QString>
int s=rand()%9+1,a=1,q=rand()%9+1;
float o=1;
int v=0;
wangge::wangge(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::wangge)
{

    ui->setupUi(this);
    resize(1000,1000);
    time = new QLabel(this);
    time->resize(400,400);
    time->move(0,400);
    time->setText("1234");
    int i=0;
    x[0]=0;
    for(i=0;i<11;i++)
    {
        x[i]=40*i;
        f[i]=400;
    }
    x[0]=0;
    for(i=0;i<11;i++)
    {
        x[i]=40*i;
        cun[i]=400;
    }
  this->startTimer(1000);
}

wangge::~wangge()
{
   // delete ui;
}

void wangge::paintEvent(QPaintEvent *)
{
    float b=0,k,c=500,u;
    int n;
    QPainter painter(this);
    QPen pen(Qt::black);
    painter.setPen(pen);
    painter.drawLine(0,0,0,400);
    painter.drawLine(0,400,400,400);
    painter.drawLine(400,0,400,400);
    painter.drawLine(400,0,0,0);
    /*painter.drawLine(500,0,500,400);
    painter.drawLine(500,400,900,400);
    painter.drawLine(900,0,900,400);
    painter.drawLine(500,0,900,0);*/
    if(a==1)
    {
        for( n=0;n<10;n++)
        {
            k=rand()%9+1;
            u=rand()%9+1;
            painter.drawLine(b,40*s,b+40,40*k);
            b=b+40;
            s=k;
            y[n]=k;
            painter.drawLine(c,40*q,c+40,40*u);
            c=c+40;
            q=u;
            t[n]=u;
        }
        a=2;
    }
    QPen bi(Qt::DotLine);
    painter.setPen(bi);
    for(n=0,b=0,c=500;n<10;n++)
    {
        b=b+40;
        c=c+40;
        painter.drawLine(0,b,400,b);
        painter.drawLine(b,0,b,400);
    }
   QPen hong(Qt::red);
    painter.setPen(hong);
    for(n=0;n<10;n++)
    {
        painter.drawLine(x[n],f[n],x[n+1],f[n+1]);
    }
    QPen lan(Qt::blue);
     painter.setPen(lan);
     for(n=0;n<10;n++)
     {
         painter.drawLine(x[n],cun[n],x[n+1],cun[n+1]);
     }
}
void wangge::timerEvent(QTimerEvent *)
{
    this->repaint();
}
void wangge::set_cpu(QString s)
{
   this->time->setText(s);
    QString p=s;
    int i;
    float q=p.toFloat();
    if(v%2==0)
    {
    for(i=0;i<10;i++)
    {
       f[i]=f[i+1];
    }
    if(o==1)
    {
       f[10]=400;
       o=2;
    }
    else
    {
       f[10]=400-4*q;
    }
    v++;
    }
    else
    {
    for(i=0;i<10;i++)
    {
       cun[i]=cun[i+1];
    }
    if(o==1)
    {
       cun[10]=400;
       o=2;
    }
    else
    {
       cun[10]=400-4*q;
    }
    v++;
    }
    this->update();
}
